package hiho;

/**
 * Created by qq65827 on 2015/2/4.
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * 小Hi最近在玩一个字符消除游戏。给定一个只包含大写字母"ABC"的字符串s，消除过程是如下进行的：
 * <p/>
 * <p/>
 * <p/>
 * 1)如果s包含长度超过1的由相同字母组成的子串，那么这些子串会被同时消除，余下的子串拼成新的字符串。例如"ABCCBCCCAA"中"CC","CCC"和"AA"会被同时消除，余下"AB"和"B"拼成新的字符串"ABB"。
 * <p/>
 * 2)上述消除会反复一轮一轮进行，直到新的字符串不包含相邻的相同字符为止。例如”ABCCBCCCAA”经过一轮消除得到"ABB"，再经过一轮消除得到"A"
 * <p/>
 * <p/>
 * <p/>
 * 游戏中的每一关小Hi都会面对一个字符串s。在消除开始前小Hi有机会在s中任意位置(第一个字符之前、最后一个字符之后以及相邻两个字符之间)插入任意一个字符('A','B'或者'C')，得到字符串t。t经过一系列消除后，小Hi的得分是消除掉的字符的总数。
 * <p/>
 * <p/>
 * <p/>
 * 请帮助小Hi计算要如何插入字符，才能获得最高得分。
 * <p/>
 * <p/>
 * <p/>
 * 输入
 * 输入第一行是一个整数T(1<=T<=100)，代表测试数据的数量。
 * <p/>
 * 之后T行每行一个由'A''B''C'组成的字符串s，长度不超过100。
 * <p/>
 * <p/>
 * <p/>
 * 输出
 * 对于每一行输入的字符串，输出小Hi最高能得到的分数。
 * <p/>
 * <p/>
 * <p/>
 * 提示
 * 第一组数据：在"ABCBCCCAA"的第2个字符后插入'C'得到"ABCCBCCCAA"，消除后得到"A"，总共消除9个字符(包括插入的'C')。
 * <p/>
 * 第二组数据："AAA"插入'A'得到"AAAA"，消除后得到""，总共消除4个字符。
 * <p/>
 * 第三组数据：无论是插入字符后得到"AABC","ABBC"还是"ABCC"都最多消除2个字符。
 * <p/>
 * <p/>
 * <p/>
 * 样例输入
 * 3
 * ABCBCCCAA
 * AAA
 * ABC
 * 样例输出
 * 9
 * 4
 * 2
 */
public class StringABC {

    public static StringBuilder[] answer;
    public static int[] resultSet;
    public static int start = -1;
    public static int num = 1;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int times = Integer.valueOf(scanner.nextLine());
        if (times > 100 || times < 1) {
            throw new IllegalArgumentException("number is not in 1 and 100");
        }
        resultSet = new int[times];
        String[] question = new String[times];
         answer = new StringBuilder[times];

        for (int i = 0; i < times; i++) {
            question[i] = scanner.nextLine();
            if (question[i].length() > 100) {
                throw new IllegalArgumentException("string is not bigger than 100");
            }
        }
        //到这里循环结束，数组已经输入完毕
        for (int i = 0; i < times; i++) {
            String test = question[i];
            Map<String, Integer> map = new HashMap<>();
            //flag的首位是start 第二位是数量
            map.put("start", start);
            map.put("num", num);
            int[] compare = new int[test.length()];

            for (int k = 0; k < test.length(); k++) {
                StringBuilder stringBuilder = new StringBuilder(test);
                stringBuilder.insert(k, "A");
                int result = findNum(stringBuilder, map);
                compare[k] = result;
            }
            int maxA = compare[0];

            for (int l = 0; l < compare.length; l++) {
                if (compare[l] > maxA) {
                    maxA = compare[l];
                }
            }

            for (int k = 0; k < test.length(); k++) {
                StringBuilder stringBuilder = new StringBuilder(test);
                stringBuilder.insert(k, "B");
                int result = findNum(stringBuilder, map);
                compare[k] = result;
            }
            int maxB = compare[0];
            for (int l = 0; l < compare.length; l++) {
                if (compare[l] > maxA) {
                    maxB = compare[l];
                }
            }
            if (maxA < maxB) {
                maxA = maxB;
            }
            for (int k = 0; k < test.length(); k++) {
                StringBuilder stringBuilder = new StringBuilder(test);
                stringBuilder.insert(k, "C");
                int result = findNum(stringBuilder, map);
                compare[k] = result;
            }
            int maxC = compare[0];
            for (int l = 0; l < compare.length; l++) {
                if (compare[l] > maxA) {
                    maxC = compare[l];
                }
            }
            if (maxC > maxA) {
                maxA = maxC;
            }
            resultSet[i] = maxA;
        }

        //System.out.println(Arrays.deepToString(answer));
        //System.out.println(Arrays.toString(resultSet));
        for (int i = 0; i < resultSet.length; i++) {
            System.out.println(resultSet[i]);
        }

    }

    public static int findNum(StringBuilder stringBuilder,Map<String,Integer> map) {
            int result = 0;
            char temp = stringBuilder.charAt(0);
            // abcbbca
            for (int j = 1; j < stringBuilder.length(); j++) {
                int start = map.get("start");
                int num = map.get("num");

                if (stringBuilder.charAt(j) == temp) {
                    if (start == -1) {
                        start = j - 1;
                        num++;
                        map.put("start", start);
                        map.put("num", num);
                    } else {
                        map.put("num", ++num);
                    }
                } else {
                    if (start != -1) {
                        result += num;
                        stringBuilder.delete(start, start + num);
                        j = 0;
                        map.put("start", -1);
                        map.put("num", 1);
                        temp = stringBuilder.charAt(0);
                    } else {
                        temp = stringBuilder.charAt(j);
                    }
                }
            }
            start = map.get("start");
            num = map.get("num");
            if (start != -1) {
                result += num;
                stringBuilder.delete(start, start + num);
                start = -1;
                num = 1;
                map.put("start", start);
                map.put("num", num);
            }

            //System.out.println(result);
            //System.out.println(stringBuilder);
            //answer[i] = stringBuilder;

        return result;
    }



}


